package com.genokon.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author by baichaoda
 * @Date 2020/7/22 17:05
 */
@RefreshScope
@RestController
public class ConfigController {
    @Value("${user.title:bai}")
    private String userName;
    @Value("${user.age:36}")
    private Integer age;

    @Resource
    private Environment environment;

    @RequestMapping("/user")
    public String simple(){
        return "hello " + userName + ", age: " +age + "!";
    }

    @RequestMapping("/env")
    public String env(){
        String envUser = this.environment.getProperty("user.title");
        int envAge = Integer.parseInt(Objects.requireNonNull(this.environment.getProperty("user.age")));
        return "hello " + envUser + ", age: " + envAge;
    }
}
